<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:vetlab="vetLabTag"
  xmlns:vetlabtags="urn:jsptagdir:/WEB-INF/tags/vetlab">
  <jsp:directive.page contentType="text/html; charset=UTF-8" session="false" isELIgnored="false" />
  <vetlabtags:page title="Amministrazione" mootools="true">
    <vetlabtags:bar />
    <vetlabtags:uleftColumn />
    <div id="urightColumn">
      <p class="Stile2">AMMINISTRAZIONE</p>
      <c:choose>
        <c:when test="${empty user}">
          <p>Effettuare il login per accedere a ques'area</p>
        </c:when>
        <c:when test="${user.admin}">
          <table id="adminUsers">
            <tr>
              <th>Email</th>
              <th>Nome</th>
              <th>Cognome</th>
              <th>Ordine vet.</th>
              <th>Nr. iscrizione</th>
              <th>Abilitato</th>
              <th>Amministratore</th>
            </tr>
            <c:forEach var="user" items="${vetlab:listUsers(pageContext)}" varStatus="loop">
              <c:set var="cssClass">
                <c:choose>
                  <c:when test="${user.enabled eq true}">enabled</c:when>
                  <c:when test="${user.enabled eq false}">disabled</c:when>
                  <c:otherwise>pending</c:otherwise>
                </c:choose>
              </c:set>
              <tr class="${cssClass}">
                <td class="email">${user.email}</td>
                <td>${user.firstName}</td>
                <td>${user.lastName}</td>
                <td>${user.city}</td>
                <td>${user.number}</td>
                <td>
                  <![CDATA[<input id="enabled_${loop.index}_true" type="radio" name="enabled_${loop.index}" value="true" class="radio" ${user.enabled eq true ? ' checked="checked"' : ''} />]]>
                  <label for="enabled_${loop.index}_true">SI</label>
                  <![CDATA[<input id="enabled_${loop.index}_false" type="radio" name="enabled_${loop.index}" value="false" class="radio" ${user.enabled eq false ? ' checked="checked"' : ''} />]]>
                  <label for="enabled_${loop.index}_false">NO</label>
                </td>
                <td>
                  <![CDATA[<input id="admin_${loop.index}" type="checkbox" name="admin_${loop.index}" value="true" class="checkbox" ${user.admin eq true ? ' checked="checked"' : ''} />]]>
                </td>
              </tr>
            </c:forEach>
          </table>
          <form id="adminForm" method="post" action="${ctx}${uri}">
            <div>
              <input type="hidden" name="editUser" value="true" />
              <input type="hidden" name="email" />
            </div>
          </form>
          <script type="text/javascript"><![CDATA[
function editUser(input, name, value){
  var email = document.id(input).getParent('tr').getElement('.email').get('text');
  var form = document.id('adminForm');
  document.id(form['email']).set('value', email);
  form.grab(new Element('input', {type: 'hidden', name: name, value: value}));
  form.submit();
}

function enabledClick(e){
  var radio = document.id(e.target);
  editUser(radio, 'enabled', radio.get('value'));
}

function adminChange(e){
  var checkbox = document.id(e.target);
  editUser(checkbox, 'admin', checkbox.checked);
}

window.addEvent('domready', function(){
  var adminUsers = document.id('adminUsers');
  adminUsers.getElements('input[name^=enabled_]').each(function(item){
    item.addEvent('click', enabledClick);
  });
  adminUsers.getElements('input[name^=admin_]').each(function(item){
    item.addEvent('change', adminChange);
  });
});]]>
          </script>
        </c:when>
        <c:otherwise>
          <p>Permesso negato</p>
        </c:otherwise>
      </c:choose>
    </div>
    <vetlabtags:lleftColumn />
  </vetlabtags:page>
</jsp:root>
